<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('隧道名称')" align='center' min-width="200px">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本端地址')" align='center' min-width="200px">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.local_ip" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column :label="$t('远端地址')" align='center' min-width="200px">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.remote_ip" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-size="size" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <ipv6over4Dialog v-if="vlanDialogVisible" ref="refVlanDialogVisible"></ipv6over4Dialog>
  </div>
</template>
<script>
import { getTunnelArray, del_info } from '@/api/network/tunnel/tunnel'
import ipv6over4Dialog from './components/ipv6over4Dialog'
export default {
  name: 'Tunnel',
  components: {
    ipv6over4Dialog
  },
  data () {
    return {
      vlanDialogVisible: false,
      tableData: [],
      tableDataAll: [],
      name: '',
      form: {
        name: 'test',
        category_info: [
          {
            'action': 'drop',
            'name': 'TCP FLOOD',
            'protype': 'tcp',
            'sid': 0,
            'status': 'on',
            'threshold': 2000,
            'type': 'detect',
            'unit': 'pps',
            'window': 200
          }
        ]
      },
      total: null,
      page: 1,
      size: 20
    }
  },
  mounted () {
    this.listData()
  },
  methods: {
    handleSizeChange (val) {
      this.size = val
      this.pageListData()
    },
    handleCurrentChange (val) {
      this.page = val
      this.pageListData()
    },
    pageListData () {
      let newPage = Math.ceil(this.total / this.size)
      if (newPage < this.page) {
        this.page = newPage
      }
      if (this.page === 0) {
        this.tableData = this.tableDataAll
      } else {
        this.tableData = this.tableDataAll.slice((this.page - 1) * this.size, this.page * this.size)
      }
    },
    // 多选按钮
    handleSelectionChange (data) {
      var name = ''
      for (var item of data) {
        if (name === '') {
          name = item.name
        } else {
          name = name + '@#@' + item.name
        }
      }
      this.name = name
    },
    // 列表
    listData () {
      getTunnelArray({ type: '6over4' }).then(res => {
        if (res.status === 0) {
          this.tableDataAll = res.info
          this.total = this.tableDataAll.length
          this.pageListData()
        }
      })
    },
    // 添加按钮表单
    dialogVisible () {
      this.vlanDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refVlanDialogVisible.init()
      })
    },
    deleteData () {
      if (this.name) {
        this.$confirm(this.$t('您是否要删除所有选中的数据?'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          del_info({ name: this.name }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.name = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    nameFormatter (data) {
      var str = data.name[0].strPhysicsName
      data.name.forEach((val, index) => {
        if (index > 0) {
          str += ',' + val.strPhysicsName
        }
      })
      return str
    }
  }
}
</script>
